<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="nickName">
        <el-input v-model="queryParams.params.nickName" placeholder="请输入用户姓名" clearable/>
      </el-form-item>
      <el-form-item label="考试" prop="bankId">
        <el-select v-model="queryParams.bankId" placeholder="请选择考试" clearable>
          <el-option v-for="item in question_bankList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.ex_study_reload_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['ex:question_bank:add']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ex_study_reloadList" @selection-change="handleSelectionChange" @sort-change="sortChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户" align="center" prop="user.nickName"/>
      <el-table-column label="考试" align="center" prop="bankName"/>
      <el-table-column label="考试日期" align="center" prop="exDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.exDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考试时间范围" align="center" prop="exTime">
        <template slot-scope="scope">
          <el-tag type="info" size="mini" effect="plain">{{scope.row.exTimeObj.name}}</el-tag>
          <el-tag type="warning" size="mini" effect="plain">
            {{scope.row.exTimeObj.time[0]}} ~ {{scope.row.exTimeObj.time[1]}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="对题数" align="center" prop="right"/>
      <el-table-column label="得分" align="center" prop="score" sortable="custom"/>
<!--      <el-table-column label="答案列表" align="center" prop="answers"/>-->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ex_study_reload_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown v-if="scope.row.status == '0'" size="mini" @command="(command) => checkStatusCommand(command, scope.row)" v-hasPermi="['pv:ex_study_reload:edit']">
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">审核</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="1" icon="el-icon-circle-check" v-hasPermi="['pv:ex_study_reload:edit']">通过</el-dropdown-item>
              <el-dropdown-item command="2" icon="el-icon-circle-close" v-hasPermi="['pv:ex_study_reload:edit']">不通过</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-button v-if="scope.row.status == '3'" size="mini" type="text" icon="el-icon-view" @click="$router.push('/ex/test_bank-do_ex_test/index/' + scope.row.id + '/' + 1)" v-hasPermi="['pv:ex_study_reload:list']">查看试卷</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['pv:ex_study_reload:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改考试报名记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="题库编号" prop="bankId">
          <el-input v-model="form.bankId" placeholder="请输入题库编号"/>
        </el-form-item>
        <el-form-item label="是否是初训" prop="isFirst">
          <el-input v-model="form.isFirst" placeholder="请输入是否是初训"/>
        </el-form-item>
        <el-form-item label="考试日期" prop="exDate">
          <el-date-picker clearable v-model="form.exDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择考试日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="考试时间范围" prop="exTime">
          <el-input v-model="form.exTime" placeholder="请输入考试时间范围"/>
        </el-form-item>
<!--        <el-form-item label="对题数" prop="right">-->
<!--          <el-input v-model="form.right" placeholder="请输入对题数"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="得分" prop="score">-->
<!--          <el-input v-model="form.score" placeholder="请输入得分"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="答案列表" prop="answers">-->
<!--          <el-input v-model="form.answers" type="textarea" placeholder="请输入内容"/>-->
<!--        </el-form-item>-->
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.ex_study_reload_status" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEx_study_reload, delEx_study_reload,
  addEx_study_reload,
  updateEx_study_reload
} from "@/api/ex/ex_study_reload";
import { listQuestion_bank_ex} from "@/api/ex/question_bank";
import { parseTime} from "@/utils/ruoyi";

export default {
  name: "Ex_study_reload",
  dicts: ['ex_study_reload_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考试报名记录表格数据
      ex_study_reloadList: [],
      question_bankList: [],//考试列表
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuId: null,
        deptId: null,
        bankId: null,
        isFirst: null,
        exDate: null,
        exTime: null,
        right: null,
        score: null,
        answers: null,
        status: null,
        reservedOne: null,
        reservedTwo: null,
        params:{}
      },
      // 表单参数
      form: {
        params:{},
      },
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getQuestionBankList();
  },
  methods: {
    /** 排序改变事件 */
    sortChange(column){
      this.queryParams.params.scoreOrder = (column.order == 'ascending') ? 'ASC' : 'DESC';
      this.getList()
    },
    /** 查询题库列表 */
    getQuestionBankList() {
      listQuestion_bank_ex({trainFlag: 0}).then(response => {
        this.question_bankList = response.rows;
      });
    },
    /** 查询考试报名记录列表 */
    getList() {
      this.loading = true;
      // this.queryParams.params['isFirst'] = 0; //todo 修改
      listEx_study_reload(this.queryParams).then(response => {
        this.ex_study_reloadList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stuId: null,
        deptId: null,
        bankId: null,
        isFirst: null,
        exDate: null,
        exTime: null,
        right: null,
        score: null,
        answers: null,
        status: null,
        reservedOne: null,
        reservedTwo: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考试报名记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.form = {...row};
      this.open = true;
      this.title = "修改考试报名记录";
      // getEx_study_reload(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改考试报名记录";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEx_study_reload(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEx_study_reload(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 核准弹窗 */
    checkStatusCommand(command, row){
      var obj = {...row}
      obj.status = command
      updateEx_study_reload(obj).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除考试报名记录编号为"' + ids + '"的数据项？').then(function () {
        return delEx_study_reload(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pv/ex_study_reload/export-ex', {
        ...this.queryParams
      }, `成绩单_${parseTime(new Date,'{y}-{m}-{d}')}.xlsx`)
    }
  }
};
</script>
